package com.ys.peony.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.AppointModel;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.ys.peony.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * 保存预约
 * Created by yml on 17/5/29.
 */

public class AddAppointActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView titleNameTv;

    @BindView(R.id.commit_btn)
    Button commitBtn;

    @BindView(R.id.add_name_et)
    EditText nameET;
    @BindView(R.id.add_phone_et)
    EditText phoneET;
    @BindView(R.id.add_identification_et)
    EditText idCardET;
    @BindView(R.id.add_car_brand_et)
    EditText carBranchET;
    @BindView(R.id.add_carnum_et)
    EditText carNumET;

    private boolean addFlag = false;
    private AppointModel model;

    @Override
    protected int getContentResId() {
        return R.layout.activity_add_appoint;
    }

    @Override
    protected void initView() {

        backBtn.setOnClickListener(this);
        commitBtn.setOnClickListener(this);

        Bundle bd = getIntent().getExtras();
        if (bd != null) {
            addFlag = bd.getBoolean("addFlag");
            model = (AppointModel) bd.getSerializable("appointModel");
        }
        if (!addFlag) {
            initAppointView();
            titleNameTv.setText(getResources().getString(R.string.appoint_detail_title));
        } else {
            titleNameTv.setText(getResources().getString(R.string.appoint_add_title));
        }
    }

    /**
     * 初始化抵押物界面
     */
    private void initAppointView() {
        nameET.setText(model.getName());
        phoneET.setText(model.getPhone());
        idCardET.setText(model.getId_card());
        carNumET.setText(model.getPlate_number());
        carBranchET.setText(model.getBrand_name());

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commit_btn:
                commit();
                break;
            case R.id.back_btn:
                closeKeyboard();
                finish();
                break;
        }
    }


    /**
     * 提交抵押物新增信息
     */
    private void commit() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }

        if (StringUtils.isEmpty(nameET.getText().toString())) {
            ToastUtils.showToast(this, "请填写姓名");
            return;
        }
        if (StringUtils.isEmpty(phoneET.getText().toString())) {
            ToastUtils.showToast(this, "请填写手机号");
            return;
        }
        if (StringUtils.isEmpty(idCardET.getText().toString())) {
            ToastUtils.showToast(this, "请填写身份证");
            return;
        }
        String idCardStr = idCardET.getText().toString();
        if (!CommonUtils.isIdentityCard(idCardStr)) {
            ToastUtils.showToast(this, "请填写正确的身份证号");
            return;
        }
        if (StringUtils.isEmpty(carNumET.getText().toString())) {
            ToastUtils.showToast(this, "请填写车辆品牌");
            return;
        }

        if (StringUtils.isEmpty(carNumET.getText().toString())) {
            ToastUtils.showToast(this, "请填写车牌号");
            return;
        }


        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();

        if (!addFlag) {
            params.put("id", model.getId());
        }
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));//(必传) Long  用户主键
        params.put("ptype", "1");//(必传)产品类型，固定值传1，表示车抵贷
        params.put("name", nameET.getText().toString());//(必传)
        params.put("id_card", idCardET.getText().toString());//(必传)
        params.put("phone", phoneET.getText().toString());
        params.put("plate_number", carNumET.getText().toString());//(必传)
        params.put("brand_name", carBranchET.getText().toString());//

        LogUtil.i("aaaa", "*****params:" + params.toString());

        if (addFlag) {
            httpManager.get(HttpURL.saveBusiAppointment, params, new BaseRespCallBack(AddAppointActivity.this) {

                public void onRespSuccess(String respStr) {
                    LogUtil.i("aaaa", "___list_" + respStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showToast(AddAppointActivity.this, "添加成功");
                            Intent it = new Intent();
                            setResult(RESULT_OK, it);
                            finish();
                        }
                    });
                }
            });
        } else {
            httpManager.get(HttpURL.updateBusiAppointment, params, new BaseRespCallBack(AddAppointActivity.this) {

                public void onRespSuccess(String respStr) {
                    LogUtil.i("aaaa", "___list_" + respStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showToast(AddAppointActivity.this, "修改成功");
                            Intent it = new Intent();
                            setResult(RESULT_OK, it);
                            finish();
                        }
                    });
                }

                @Override
                public void onRespFailure(String msg) {
                    ToastUtils.showToast(AddAppointActivity.this, msg);
                }

            });
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
